<template>
    <el-card class="right-content user-management">
        <h3>用户管理</h3>
        <suspense>
          <XTable 
              ref="TableRef"
              :tableConfigure="tableConfigure" 
              :searchList="searchList" 
              :buttonObject="buttonObject" 
              :url="fetchApi.getUserList"
              :pagination="true"
          />
        </suspense>
    </el-card>
    <el-dialog
      title="添加用户"
      v-model="dialogVisible"
      width="30%"
    >
    <el-form :model="formModel">
        <el-form-item label="姓名:" name="nickName">
          <el-input v-model="formModel.nickName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="电话:" name="mobile">
          <el-input v-model="formModel.mobile" placeholder="请输入用户电话" />
        </el-form-item>
        <el-form-item label="邮箱:" name="email">
          <el-input v-model="formModel.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="角色:" name="role">
          <el-select
            style="width: 100%"
            v-model="formModel.role"
            placeholder="请选择角色"
          >
            <el-option
                v-for="item in roleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
            </el-select>
        </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitHandler">
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import XTable from '@/components/XTable/index.vue'
import {tableConfigure} from './constant'
import fetchApi from '@/api/user'
import { useRequest } from 'vue-request'

const searchList = [
            {
                type: 'select',
                prop: 'isAdmin',
                options: [{
                    label: '管理员',
                    value: 0
                },{
                    label: '普通用户',
                    value: 1
                }]
            }, {
                type: 'input',
                prop: 'userName'
            }
        ]
const buttonObject = {
    text: '添加用户',
    handleClick: ()=> {
        dialogVisible.value = true
    },
    permission: ['app:user:add']
}

const TableRef = ref<{ refresh: () => void }>()
const refresh = () => TableRef.value?.refresh()

let dialogVisible = ref(false)
let formModel = reactive({
    nickName: '',
    mobile: '',
    email: '',
    role: '',
})
let roleOptions = [
    {label: 0, value: 0},
    {label: 1, value: 1},
]
const submitHandler = async () => {
    console.log(formModel);
    await useRequest(fetchApi.addUser, {
        defaultParams: [formModel]
    })
    refresh()
    dialogVisible.value = false
}
</script>

<style lang="less" scoped>
.user-management {
    h3 {
        text-align: left;
        font-size: 18px;
        font-weight: 600;
    }
}
</style>